package com.zh.work.controller.platform.trans;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.entity.JSONResult;
import com.zh.common.util.DateSetUtil;
import com.zh.work.entity.trans.TransBackupEntity;
import com.zh.work.entity.trans.TransRealEntity;
import com.zh.work.entity.trans.TransTodayEntity;
import com.zh.work.service.platform.trans.ErrorTransService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 异常交易查询
 * @Author: Li.HuiNeng
 * @Create in: 2020-01-07 17:11
 * @Version: 1.0.1
 */
@Api(value = "异常交易控制器", produces = "application/json", tags = {"异常交易控制器"})
@RestController
@RequestMapping("/errTrans")
public class ErrorTransController {

    @Autowired
    private ErrorTransService service;

    @ApiOperation(value = "分页查询实时交易数据集", tags = {"getErrorTransTodayPages"}, notes = "分页查询实时交易数据集")
    @GetMapping("/getErrorTransTodayPages")
    public JSONResult<List<TransTodayEntity>> getErrorTransTodayPages(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageNo") Integer pageSize,
            @ApiParam(value = "实时交易实体信息", name = "param") TransTodayEntity param) {
        JSONResult<List<TransTodayEntity>> jsonResult = new JSONResult<>();
        try {
            Page<TransTodayEntity> page = new Page<>(pageNo, pageSize);
            IPage<TransTodayEntity> result = service.getErrorTransTodayPages(page, param);
            jsonResult.setTotal(page.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页查询历史交易数据集", tags = {"getTransBackupTodayPages"}, notes = "分页查询实时交易数据集")
    @GetMapping("/getTransBackupTodayPages")
    public JSONResult<List<TransBackupEntity>> getTransBackupTodayPages(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageNo") Integer pageSize,
            @ApiParam(value = "历史交易实体信息", name = "param") TransBackupEntity param) {
        JSONResult<List<TransBackupEntity>> jsonResult = new JSONResult<>();
        try {
            Page<TransBackupEntity> page = new Page<>(pageNo, pageSize);
            IPage<TransBackupEntity> result = service.getTransBackupTodayPages(page, param);
            jsonResult.setTotal(page.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页查询冲正交易数据集", tags = {"getTransRealPages"}, notes = "分页查询冲正交易数据集")
    @GetMapping("/getTransRealPages")
    public JSONResult<List<TransRealEntity>> getTransRealPages(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageNo") Integer pageSize,
            @ApiParam(value = "冲正交易实体信息", name = "param") TransRealEntity param) {
        JSONResult<List<TransRealEntity>> jsonResult = new JSONResult<>();
        try {
            Page<TransRealEntity> page = new Page<>(pageNo, pageSize);
            IPage<TransRealEntity> result = service.getTransRealPages(page, param);
            jsonResult.setTotal(page.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页查询备份交易数据集", tags = {"getTransTodayPages"}, notes = "分页查询备份交易数据集")
    @GetMapping("/getTransTodayPages")
    public JSONResult<List<TransTodayEntity>> getTransTodayPages(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageNo") Integer pageSize,
            @ApiParam(value = "实时交易实体信息", name = "param") TransTodayEntity param) {
        JSONResult<List<TransTodayEntity>> jsonResult = new JSONResult<>();
        try {
            Page<TransTodayEntity> page = new Page<>(pageNo, pageSize);
            IPage<TransTodayEntity> result = service.getTransTodayPages(page, param);
            jsonResult.setTotal(page.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "查询可疑交易统计数据", tags = {"getErrorTransStatistic"}, notes = "查询可疑交易统计数据")
    @GetMapping("/getErrorTransStatistic")
    public JSONResult<Map<String, String>> getErrorTransStatistic(@ApiParam(value = "实时交易实体信息", name = "param") TransTodayEntity param) {
        JSONResult<Map<String, String>> jsonResult = new JSONResult<>();
        Map<String, String> map = null;
        if (Objects.isNull(param.getStartDateTime())) {
            param.setStartDateTime(DateSetUtil.getDayFirstSec(new Date()));
        }
        if (Objects.isNull(param.getEndDateTime())) {
            param.setEndDateTime(DateSetUtil.getDayLastSec(new Date()));
        }
        if (Objects.isNull(param.getTransStartTime())) {
            param.setTransStartTime(DateSetUtil.getDayLastSec(new Date()));
        }
        if (Objects.isNull(param.getTransEndTime())) {
            param.setTransEndTime(DateSetUtil.getDayLastSec(new Date()));
        }
        try {
            map = this.service.getErrorTransStatistic(param);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
        }
        if (map != null) {
            if (map.size() > 0) {
                jsonResult.setSuccess(true);
                jsonResult.setData(map);
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("统计数量为0");
            }
        } else {
            jsonResult.setSuccess(false);
            jsonResult.setMessage("统计数量为空");
        }
        return jsonResult;
    }


    @ApiOperation(value = "查询可疑交易统计备份数据", tags = {"getErrorTransRealStatistic"}, notes = "查询可疑交易统计备份数据")
    @GetMapping("/getErrorTransRealStatistic")
    public JSONResult<List<Map<String, String>>> getErrorTransRealStatistic(@ApiParam(value = "实时交易实体信息", name = "param") TransTodayEntity param) {
        JSONResult<List<Map<String, String>>> jsonResult = new JSONResult<>();
        List<Map<String, String>> map = null;
        if (Objects.isNull(param.getStartDateTime())) {
            param.setStartDateTime(DateSetUtil.getDayFirstSec(new Date()));
        }
        if (Objects.isNull(param.getEndDateTime())) {
            param.setEndDateTime(DateSetUtil.getDayLastSec(new Date()));
        }
        if (Objects.isNull(param.getTransStartTime())) {
            param.setTransStartTime(DateSetUtil.getDayLastSec(new Date()));
        }
        if (Objects.isNull(param.getTransEndTime())) {
            param.setTransEndTime(DateSetUtil.getDayLastSec(new Date()));
        }
        try {
            map = this.service.getErrorTransRealStatistic(param);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
        }
        if (map != null) {
            if (map.size() > 0) {
                jsonResult.setSuccess(true);
                jsonResult.setData(map);
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("统计数量为0");
            }
        } else {
            jsonResult.setSuccess(false);
            jsonResult.setMessage("统计数量为空");
        }
        return jsonResult;
    }

}
